package com.buercorp.wangyu.core1.ch12.threads.threadsPool;

import java.util.concurrent.*;

/**
 * 线程池处理 Callable 任务
 * @author liyiyu
 */
public class ThreadCallableText2 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 通过 ThreadPoolExecutor创建线程池
         * public ThreadPoolExecutor(int corePoolSize,
         *                               int maximumPoolSize,
         *                               long keepAliveTime,
         *                               TimeUnit unit,
         *                               BlockingQueue<Runnable> workQueue) {
         *         this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         *              Executors.defaultThreadFactory(), defaultHandler);
         *     }
          */
//        BlockingQueue
        ThreadPoolExecutor pool = new ThreadPoolExecutor(3, // 核心
                5,
                6,
                TimeUnit.SECONDS, // 单位时间
                new ArrayBlockingQueue<>(4), // 任务队列
                Executors.defaultThreadFactory(), // 创建线程工厂
                new ThreadPoolExecutor.AbortPolicy() // 任务拒绝策略
        );

        // 使用线程处理多任务
        Future<String> f1 = pool.submit(new MyCallable(100));
        Future<String> f2 = pool.submit(new MyCallable(200));
        Future<String> f3 = pool.submit(new MyCallable(300));
        Future<String> f4 = pool.submit(new MyCallable(400));



        System.out.println(f1.get());
        System.out.println(f2.get());
        System.out.println(f3.get());
        System.out.println(f4.get());
    }
}
